<template>
  <div class="student-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="250px" class="sidebar">
        <div class="sidebar-header">
          <el-avatar :size="60" class="user-avatar">
            <el-icon><User /></el-icon>
          </el-avatar>
          <div class="user-info">
            <div class="user-name">{{ userStore.userInfo.student?.name || '学生' }}</div>
            <div class="user-role">学生</div>
          </div>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          router
          unique-opened
        >
          <el-menu-item index="/student">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-menu-item index="/student/activities">
            <el-icon><Calendar /></el-icon>
            <span>活动列表</span>
          </el-menu-item>
          
          <el-menu-item index="/student/registrations">
            <el-icon><List /></el-icon>
            <span>我的报名</span>
          </el-menu-item>
          
          <el-menu-item index="/student/feedback">
            <el-icon><ChatDotRound /></el-icon>
            <span>我的反馈</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主要内容区域 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="main-header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/student' }">学生端</el-breadcrumb-item>
              <el-breadcrumb-item v-if="breadcrumbItems.length > 0">
                {{ breadcrumbItems[breadcrumbItems.length - 1] }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <!-- 通知 -->
            <el-badge :value="unreadNotifications" class="notification-badge">
              <el-button 
                circle 
                @click="showNotifications = true"
                :icon="Bell"
              />
            </el-badge>
            
            <!-- 用户菜单 -->
            <el-dropdown @command="handleCommand">
              <el-button circle :icon="Setting" />
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    个人信息
                  </el-dropdown-item>
                  <el-dropdown-item command="settings">
                    <el-icon><Setting /></el-icon>
                    设置
                  </el-dropdown-item>
                  <el-dropdown-item command="logout" divided>
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 主要内容 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
    
    <!-- 通知抽屉 -->
    <el-drawer
      v-model="showNotifications"
      title="通知消息"
      direction="rtl"
      size="400px"
    >
      <div class="notifications-content">
        <div v-if="notifications.length === 0" class="no-notifications">
          <el-empty description="暂无通知" />
        </div>
        <div v-else>
          <div 
            v-for="notification in notifications" 
            :key="notification.id"
            class="notification-item"
            :class="{ 'unread': !notification.read }"
            @click="markAsRead(notification.id)"
          >
            <div class="notification-header">
              <span class="notification-title">{{ notification.title }}</span>
              <span class="notification-time">{{ formatRelativeTime(notification.createdAt) }}</span>
            </div>
            <div class="notification-content">{{ notification.content }}</div>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { formatRelativeTime } from '@/utils'
import { ElMessage } from 'element-plus'
import {
  User,
  House,
  Calendar,
  List,
  ChatDotRound,
  Bell,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'
import api from '@/utils/api'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const showNotifications = ref(false)
const notifications = ref([])

const activeMenu = computed(() => {
  return route.path
})

const breadcrumbItems = computed(() => {
  const pathMap = {
    '/student': [],
    '/student/activities': ['活动列表'],
    '/student/registrations': ['我的报名'],
    '/student/feedback': ['我的反馈']
  }
  
  const path = route.path
  if (path.includes('/activities/') && path !== '/student/activities') {
    return ['活动列表', '活动详情']
  }
  
  return pathMap[path] || []
})

const unreadNotifications = computed(() => {
  return notifications.value.filter(n => !n.read).length
})

const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      ElMessage.info('设置功能开发中')
      break
    case 'logout':
      userStore.logout()
      ElMessage.success('退出登录成功')
      router.push('/login')
      break
  }
}

const loadNotifications = async () => {
  try {
    const response = await api.get('/notifications')
    notifications.value = response.data.data || []
  } catch (error) {
    console.error('加载通知失败:', error)
  }
}

const markAsRead = async (notificationId) => {
  try {
    await api.put(`/notifications/${notificationId}/read`)
    const notification = notifications.value.find(n => n.id === notificationId)
    if (notification) {
      notification.read = true
    }
  } catch (error) {
    console.error('标记通知已读失败:', error)
  }
}

onMounted(() => {
  loadNotifications()
})
</script>

<style scoped>
.student-layout {
  height: 100vh;
}

.sidebar {
  background: #fff;
  border-right: 1px solid #e4e7ed;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #e4e7ed;
}

.user-avatar {
  margin-bottom: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.user-info {
  text-align: center;
}

.user-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.user-role {
  font-size: 12px;
  color: #909399;
}

.sidebar-menu {
  border-right: none;
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
  margin: 4px 12px;
  border-radius: 6px;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #f5f7fa;
}

.sidebar-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
}

.main-header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.notification-badge {
  margin-right: 8px;
}

.main-content {
  background: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
}

.notifications-content {
  padding: 0;
}

.no-notifications {
  padding: 40px 20px;
  text-align: center;
}

.notification-item {
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.3s;
}

.notification-item:hover {
  background-color: #f5f7fa;
}

.notification-item.unread {
  background-color: #f0f9ff;
  border-left: 3px solid #409eff;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.notification-title {
  font-weight: 600;
  color: #303133;
}

.notification-time {
  font-size: 12px;
  color: #909399;
}

.notification-content {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 200px !important;
  }
  
  .main-header {
    padding: 0 15px;
  }
  
  .main-content {
    padding: 15px;
  }
  
  .sidebar-header {
    padding: 15px;
  }
  
  .user-name {
    font-size: 14px;
  }
}
</style>